//
//  CIButton+layoutExtent.m
//  Speedy
//
//  Created by ideacp on 15/12/31.
//  Copyright © 2015年 Jiangcp. All rights reserved.
//

#import "CIButton+layoutExtent.h"

void UIButton_Layout_G(UIButton* btn,CIButtonLayoutType ALType,CGFloat gap, BOOL sizeFit)
{
    CGSize imageSize = btn.imageView.frame.size;
    CGSize titleSize = [[btn titleForState:UIControlStateNormal] boundingRectWithSize:CGSizeMake(NSUIntegerMax, NSUIntegerMax) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:btn.titleLabel.font?:[UIFont systemFontOfSize:14]} context:nil].size;
    
    switch (ALType) {
        case CIButtonLayoutTypeLeft:
        {
            if (sizeFit){
                CGRect rect  = {
                    .origin = btn.frame.origin,
                    .size = {
                        .width = imageSize.width + gap + titleSize.width,
                        .height = MAX(titleSize.height,imageSize.height),
                    }
                };
                btn.frame = rect;
            }
            
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, gap);
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, gap, 0, 0);
        }
            break;
        case CIButtonLayoutTypeRight:
        {
            if (sizeFit){
                CGRect rect  = {
                    .origin = btn.frame.origin,
                    .size = {
                        .width = imageSize.width + gap + titleSize.width,
                        .height = MAX(titleSize.height,imageSize.height),
                    }
                };
                btn.frame = rect;
            }
           
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, titleSize.width + gap, 0, 0);
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, 0, imageSize.width + gap);
            break;
        }
        case CIButtonLayoutTypeTop:
        {
            if (sizeFit){
                CGRect rect  = {
                    .origin = btn.frame.origin,
                    .size = {
                        .width = MAX(titleSize.width,imageSize.width),
                        .height = imageSize.height + gap + titleSize.height,
                    }
                };
                
                btn.frame = rect;
            }
            
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, titleSize.height + gap, -titleSize.width);
            btn.titleEdgeInsets = UIEdgeInsetsMake(imageSize.height + gap, -imageSize.width, 0, 0);
            break;
        }
        case CIButtonLayoutTypeBottom:
        {
            if (sizeFit){
                CGRect rect  = {
                    .origin = btn.frame.origin,
                    .size = {
                        .width = MAX(titleSize.width,imageSize.width),
                        .height = imageSize.height + gap + titleSize.height,
                    }
                };
                
                btn.frame = rect;
            }
            
            btn.imageEdgeInsets = UIEdgeInsetsMake(titleSize.height + gap,0, 0, -titleSize.width);
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, imageSize.height + gap, 0);
            break;
        }
        case CIButtonLayoutTypeCenter:
        {
            CGRect rect  = {
                .origin = btn.frame.origin,
                .size = {
                    .width = MAX(titleSize.width,imageSize.width),
                    .height = MAX(imageSize.height, titleSize.height)
                }
            };
            
            if (sizeFit){
                btn.frame = rect;
            }
            
            btn.imageEdgeInsets = UIEdgeInsetsZero;
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, -CGRectGetWidth(rect), 0, 0);
            break;
        }
        default:
            break;
    }
}

void UIButton_Layout(UIButton* btn,CIButtonLayoutType ALType, BOOL sizeFit)
{
    UIButton_Layout_G(btn,ALType,8,sizeFit);
}

void UIButton_Layout_sizeFit(UIButton* btn,CIButtonLayoutType ALType, CGFloat gap, CGSize imageSize, BOOL sizeFit) {
    if (imageSize.height == 0 || imageSize.width == 0) {
        imageSize = btn.imageView.frame.size;
    }
    
    CGSize titleSize = [[btn titleForState:UIControlStateNormal] boundingRectWithSize:CGSizeMake(NSUIntegerMax, NSUIntegerMax) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:btn.titleLabel.font?:[UIFont systemFontOfSize:14]} context:nil].size;
    
    switch (ALType) {
        case CIButtonLayoutTypeLeft:
        {
            if (sizeFit){
                btn.size = CGSizeMake(imageSize.width + gap + titleSize.width, MAX(titleSize.height,imageSize.height));
            }
            
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, gap);
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, gap, 0, 0);
        }
            break;
        case CIButtonLayoutTypeRight:
        {
            if (sizeFit){
                btn.size = CGSizeMake(imageSize.width + gap + titleSize.width, MAX(titleSize.height,imageSize.height));
            }
            
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, titleSize.width + gap, 0, 0);
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, 0, imageSize.width + gap);
            break;
        }
        case CIButtonLayoutTypeTop:
        {
            if (sizeFit){
                btn.size = CGSizeMake(MAX(titleSize.width,imageSize.width), imageSize.height + gap + titleSize.height);
            }
            
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, titleSize.height + gap, -titleSize.width);
            btn.titleEdgeInsets = UIEdgeInsetsMake(imageSize.height + gap, -imageSize.width, 0, 0);
            break;
        }
        case CIButtonLayoutTypeBottom:
        {
            if (sizeFit){
                btn.size = CGSizeMake(MAX(titleSize.width,imageSize.width), imageSize.height + gap + titleSize.height);
            }
            
            btn.imageEdgeInsets = UIEdgeInsetsMake(titleSize.height + gap,0, 0, -titleSize.width);
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, imageSize.height + gap, 0);
            break;
        }
        case CIButtonLayoutTypeCenter:
        {
            CGRect rect  = {
                .origin = btn.frame.origin,
                .size = {
                    .width = MAX(titleSize.width,imageSize.width),
                    .height = MAX(imageSize.height, titleSize.height)
                }
            };
            
            if (sizeFit){
                 btn.size = CGSizeMake(MAX(titleSize.width,imageSize.width), MAX(imageSize.height, titleSize.height));
            }
            
            btn.imageEdgeInsets = UIEdgeInsetsZero;
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, -CGRectGetWidth(rect), 0, 0);
            break;
        }
        default:
            break;
    }
}
